/**
 *@description 用户画像》购买渠道偏好
 *@author yhw
 */
import {
  tmallQuery,
  storeQuery
} from '../services/BuyChannel';
import {
  parse
} from 'qs';
import error from '../utils/tips';

export default {

  namespace: 'BuyChannel',

  state: {
    tmallData: null,
    tmallLoading: false,
    storeData: null,
    storeLoading: false
  },

  subscriptions: {},

  effects: { * tmallQuery({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'tmallLoading'
      });

      const {
        data
      } = yield call(tmallQuery, parse(payload));

      if (data) {

        let temp = {
            category: [],
            value: []
          },
          colors = ['#ceb180', '#fff2cc', '#fbe5d6', '#f8cbad', '#ededed', '#deebf7', '#bdd7ee', '#adb9ca', '#f2f2f2', '#d9d9d9', '#ffc86b', '#ffb22f'];
        data.data && data.data.map(function(item, index) {
          temp.category.push(item.name);
          temp.value.push({
            name: item.name,
            value: item.value,
            itemStyle: {
              normal: {
                color: colors[index]
              }
            }
          });
        });

        yield put({
          type: 'tmallQuerySuccess',
          payload: {
            tmallData: temp,
          },
        });
      } else {
        error("获取天猫购买偏好失败");
      }
    },
    //===================================
    * storeQuery({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'storeLoading'
      });

      const {
        data
      } = yield call(storeQuery, parse(payload));

      if (data) {

        let temp = {
            category: [],
            value: []
          },
          colors = ['#ceb180', '#fff2cc', '#fbe5d6', '#f8cbad', '#ededed', '#deebf7', '#bdd7ee', '#adb9ca', '#f2f2f2', '#d9d9d9', '#ffc86b', '#ffb22f'];
        data.data && data.data.map(function(item, index) {
          temp.category.push(item.name);
          temp.value.push({
            name: item.name,
            value: item.value,
            itemStyle: {
              normal: {
                color: colors[index]
              }
            }
          });
        });

        yield put({
          type: 'storeQuerySuccess',
          payload: {
            storeData: temp,
          },
        });
      } else {
        error("获取门店购买偏好失败");
      }
    },
  },

  reducers: {
    tmallLoading(state) {
      return {...state,
        tmallLoading: true
      };
    },
    tmallQuerySuccess(state, action) {
      return {...state,
        ...action.payload,
        tmallLoading: false
      };
    },
    //===================================
    storeLoading(state) {
      return {...state,
        storeLoading: true
      };
    },
    storeQuerySuccess(state, action) {
      return {...state,
        ...action.payload,
        storeLoading: false
      };
    },

  },
};